﻿using AdminStyem.Application.Command.ProcessingCommand;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using AutoMapper;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.ProcessingCommandHandler
{
    public class ProcessingaddCommandHandler : IRequestHandler<ProcessingaddCommand, APIResult<object>>
    {
        IBaseRespository<Processing> BaseProcessing;
        IMapper mapper;

        public ProcessingaddCommandHandler(IMapper mapper, IBaseRespository<Processing> BaseProcessing)
        {
            this.mapper = mapper;
            this.BaseProcessing = BaseProcessing;
        }

        public Task<APIResult<object>> Handle(ProcessingaddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> ret = new APIResult<object>();
            ret.Code = ResultCode.添加成功;
            ret.Message=ResultCode.添加成功.ToString();
            var entity = mapper.Map<Processing>(request);
             int i = BaseProcessing.add(entity).Result;
            if (i <= 0)
            {
                ret.Code = ResultCode.添加失败;
                ret.Message = ResultCode.添加失败.ToString();
                return Task.FromResult(ret);
            }

                return Task.FromResult(ret);
        }
    }
}
